//
//  RSWDBOperation.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/10/24.
//  Copyright © 2016年 com.rs. All rights reserved.
//

#import "RSWDBOperation.h"

@implementation RSWDBOperation

static XMSSQLiteHelper *s_sqlite = nil;

+ (void)RSWDBOperationOpen
{
    if (s_sqlite != nil)
        return;
    if (s_sqlite == nil)
        s_sqlite = [XMSSQLiteHelper new];
    NSString *szPath = [[XLGContext context].m_szUserdefaulteppath stringByAppendingPathComponent:RSWDBFE];
    [s_sqlite openTable:szPath flags:SQLITE_OPEN_READWRITE| SQLITE_OPEN_FULLMUTEX];
}

+ (void)RSWDBOperationClose
{
    [s_sqlite closeTable];
    s_sqlite = nil;
}

+ (XMSSQLiteHelper *)RSWDBOperationSQLite
{
    return s_sqlite;
}

-(XLGResult*)updateMyInfo:(MyInfo*)myInfo
{
    __block NSInteger nResult =FALSE;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    nResult = [sqlHelper transExce:^NSInteger
               {
                   XMSQueueDictionary *qdValue = [XMSSQLiteDBTable getDBTableClsValue:@"myinfo" cls:myInfo DBCol:nil];
                   XMSQueueDictionary *qdCondition = [XMSSQLiteDBTable getDBTableClsValue:@"myinfo" cls:myInfo DBCol:@[@"usernumber"]];
                   
                   nResult = [sqlHelper runUpdateOrInsertIgnore:@"myinfo" valueKv:[sqlHelper mergeSQLKv:qdValue] conditionKv:[sqlHelper mergeSQLKv:qdCondition]];
                   
                   return nResult;
               }];
    return [XLGResult resultWithOutResult:nResult];
}

-(XLGResult*)updateMyHeadImage:(NSString*)logoURL
{
    __block NSInteger nResult =FALSE;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    nResult = [sqlHelper transExce:^NSInteger
               {
                   nResult = [sqlHelper immediateRunSQL:[[NSString stringWithFormat:@"update myinfo set photo = '%@' where usernumber = %@;",logoURL,[MyInfo myInfo].userNumber] UTF8String]];
                   
                   return nResult;
               }];
    return [XLGResult resultWithOutResult:nResult];
}

-(XLGResult*)getCurrentData
{
    __block NSInteger nResult = FALSE;
    __block id myinfo = nil;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    nResult = [sqlHelper runSelectSQL:[NSString stringWithFormat:@"select * from myinfo where usernumber = %@",[XLGContext context].m_szUsernumber] withCls:[MyInfo class] completion:^(NSArray *arList)
    {
        if(arList.count > 0)
            myinfo = [arList objectAtIndex:0];
    }];

    return [XLGResult resultWithOutResult:nResult outObj:myinfo];
}

-(XLGResult*)saveEntAddressBookContact:(NSArray*)contacts
{
    NSInteger nResult =FALSE;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    nResult = [sqlHelper transExce:^NSInteger
               {
                   NSInteger nResult =FALSE;
                   nResult = [sqlHelper immediateRunSQL:[@"DELETE FROM entaddressbookcontact;" UTF8String]];
                   if(nResult == YES)
                   {
                       for(EntAddressBookContact *c in contacts)
                       {
                           XMSQueueDictionary *qdValue = [XMSSQLiteDBTable getDBTableClsValue:@"entaddressbookcontact" cls:c DBCol:nil];
                           XMSQueueDictionary *qdCondition = [XMSSQLiteDBTable getDBTableClsValue:@"entaddressbookcontact" cls:c DBCol:@[@"usernumber"]];
                           nResult = [sqlHelper runUpdateOrInsertIgnore:@"entaddressbookcontact" valueKv:[sqlHelper mergeSQLKv:qdValue] conditionKv:[sqlHelper mergeSQLKv:qdCondition]];
                       }
                   }
                   return nResult;
               }];
    return [XLGResult resultWithOutResult:nResult];
}

-(XLGResult*)deleteEntAddressBookContact:(NSArray*)contactsID
{
    NSInteger nResult =FALSE;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    
    NSString *condition = [NSString megerStringWithTag:contactsID value:^NSString *(id obj)
                           {
                               return [NSString stringWithFormat:@"usernumber = %@",[(XMSNumberInt*)obj toNSString]];
                           } tag:@" or "];
    
    NSString *sql = [NSString stringWithFormat:@"update entaddressbookcontact set status = 0 where %@",condition];
    nResult = [sqlHelper immediateRunSQL:[sql UTF8String]];
    
    return [XLGResult resultWithOutResult:nResult];
}

-(XLGResult*)saveDicInfo:(NSArray*)dics
{
    NSInteger nResult =FALSE;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    
    nResult = [sqlHelper transExce:^NSInteger
               {
                   NSInteger nResult =FALSE;
                   
                   nResult = [sqlHelper immediateRunSQL:[@"DELETE FROM dicinfo;" UTF8String]];
                   if(nResult == YES)
                   {
                       for(DicInfo *c in dics)
                       {
                           XMSQueueDictionary *qdValue = [XMSSQLiteDBTable getDBTableClsValue:@"dicinfo" cls:c DBCol:nil];
                           XMSQueueDictionary *qdCondition = [XMSSQLiteDBTable getDBTableClsValue:@"dicinfo" cls:c DBCol:@[@"dicid"]];
                           nResult = [sqlHelper runUpdateOrInsertIgnore:@"dicinfo" valueKv:[sqlHelper mergeSQLKv:qdValue] conditionKv:[sqlHelper mergeSQLKv:qdCondition]];
                       }
                   }
                   return nResult;
               }];
    return [XLGResult resultWithOutResult:nResult];
}

-(XLGResult*)saveMenuInfo:(NSArray*)dics
{
    NSInteger nResult =FALSE;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    nResult = [sqlHelper transExce:^NSInteger
               {
                   NSInteger nResult =FALSE;
                   nResult = [sqlHelper immediateRunSQL:[@"DELETE FROM menuinfo;" UTF8String]];
                   if(nResult == YES)
                   {
                       for(MenuInfo *c in dics)
                       {
                           XMSQueueDictionary *qdValue = [XMSSQLiteDBTable getDBTableClsValue:@"menuinfo" cls:c DBCol:nil];
                           XMSQueueDictionary *qdCondition = [XMSSQLiteDBTable getDBTableClsValue:@"menuinfo" cls:c DBCol:@[@"menuid"]];
                           nResult = [sqlHelper runUpdateOrInsertIgnore:@"menuinfo" valueKv:[sqlHelper mergeSQLKv:qdValue] conditionKv:[sqlHelper mergeSQLKv:qdCondition]];
                       }
                   }
                   return nResult;
               }];
    return [XLGResult resultWithOutResult:nResult];
}

-(XLGResult*)saveWorkflowInfo:(NSArray*)dics
{
    NSInteger nResult =FALSE;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    nResult = [sqlHelper transExce:^NSInteger
               {
                   NSInteger nResult =FALSE;
                   nResult = [sqlHelper immediateRunSQL:[@"DELETE FROM workflowinfo;" UTF8String]];
                   if(nResult == YES)
                   {
                       for(WorkflowInfo *c in dics)
                       {
                           XMSQueueDictionary *qdValue = [c tableClassValue:nil];
                           XMSQueueDictionary *qdCondition = [c tableClassValue:@[@"processguid"]]; 
                           nResult = [sqlHelper runUpdateOrInsertIgnore:@"workflowinfo" valueKv:[sqlHelper mergeSQLKv:qdValue] conditionKv:[sqlHelper mergeSQLKv:qdCondition]];
                       }
                   }
                   return nResult;
               }];
    return [XLGResult resultWithOutResult:nResult];
}

-(XLGResult*)saveDeptInfo:(NSArray*)dics
{
    NSInteger nResult =FALSE;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    nResult = [sqlHelper transExce:^NSInteger
               {
                   NSInteger nResult =FALSE;
                   nResult = [sqlHelper immediateRunSQL:[@"DELETE FROM deptinfo;" UTF8String]];
                   if(nResult == YES)
                   {
                       for(DeptInfo *c in dics)
                       {
                           XMSQueueDictionary *qdValue = [c tableClassValue:nil];
                           XMSQueueDictionary *qdCondition = [c tableClassValue:@[@"deptid"]];
                           nResult = [sqlHelper runUpdateOrInsertIgnore:@"deptinfo" valueKv:[sqlHelper mergeSQLKv:qdValue] conditionKv:[sqlHelper mergeSQLKv:qdCondition]];
                       }
                   }
                   return nResult;
               }];
    return [XLGResult resultWithOutResult:nResult];
}


-(XLGResult*)getEntAddressBookContactDetialWithContactNumber:(NSArray*)contactNumbers
{
    NSInteger nResult =FALSE;
    __block NSArray *thisList = nil;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    nResult = [sqlHelper runSelectSQL:[NSString stringWithFormat:@"select * from entaddressbookcontact where %@",[NSString megerStringWithTag:contactNumbers value:^NSString *(id obj){
         return [NSString stringWithFormat:@"usernumber = %@",obj];
        } tag:@" or "]] withCls:[EntAddressBookContact class] completion:^(NSArray *arList)
               {
                   thisList = arList;
               }];
    return [XLGResult resultWithOutResult:nResult outObj:thisList];
}

-(XLGResult*)getEntAddressBookContact
{
    NSInteger nResult =FALSE;
    __block NSArray *thisList = nil;
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc]initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    nResult = [sqlHelper runSelectSQL:@"select * from entaddressbookcontact where 1=1" withCls:[EntAddressBookContact class] completion:^(NSArray *arList)
               {
                   thisList = arList;
               }];
    return [XLGResult resultWithOutResult:nResult outObj:thisList];
}

//获取字典数据
-(XLGResult*)getDictionaryInfoWithTypeId:(NSString*)typeId
{
    NSInteger nResult = FALSE;
    __block NSArray *thisList = nil;
    
    NSString *sql = [NSString stringWithFormat:@"select * from dicinfo where upid in(select dicid from dicinfo where bindflag = '%@') order by orderindex",typeId];
    
    XMSSQLiteHelper *sqlHelper = [[XMSSQLiteHelper alloc] initWithSQLiteHelper:[RSWDBOperation RSWDBOperationSQLite]];
    
    nResult = [sqlHelper runSelectSQL:sql withCls:[DicInfo class] completion:^(NSArray *arList)
               {
                   thisList = arList;
               }];
    return [XLGResult resultWithOutResult:nResult outObj:thisList];
}
@end
